var pageMod = require("page-mod");
var widgets = require("widget");
var tabs = require("tabs");
var data = require("self").data;

var ss = require("simple-storage");

function init_storage()
{
    if (!ss.storage.rm_main_ad)
        ss.storage.rm_main_ad = 0;
    if (!ss.storage.rm_bbs_rules)
        ss.storage.rm_bbs_rules = 0;
    if (!ss.storage.hide_level_2)
        ss.storage.hide_level_2 = 0;
    if (!ss.storage.blk_persons)
        ss.storage.blk_persons = "";
    if (!ss.storage.blk_signs)
        ss.storage.blk_signs = "";
    if (!ss.storage.blk_heads)
        ss.storage.blk_heads = "";
    if (!ss.storage.blk_pics)
        ss.storage.blk_pics = "";
    if (!ss.storage.blk_articles)
        ss.storage.blk_articles = "";
    if (!ss.storage.inner)
        ss.storage.inner = "";
    if (!ss.storage.outter)
        ss.storage.outter = "";
    if (!ss.storage.auto_domain)
        ss.storage.auto_domain = "";
    if (!ss.storage.keep_login)
        ss.storage.keep_login = "";
    if (!ss.storage.login_nowait)
        ss.storage.login_nowait = "";
    if (!ss.storage.in_uestc)
        ss.storage.in_uestc = 0;
}

init_storage();

var widget = widgets.Widget({
    label: "CleanQSH",
    id: "text-entry",
    contentURL: data.url("images/qsh.png"),
    contentScriptFile: data.url("js/proc/click-listener.js")
});

widget.port.on("left-click", function(){
    tabs.open("http://bbs.stuhome.net/");
});

widget.port.on("right-click", function(){
    tabs.open(data.url("config.html"));
});

function getDomain(url)
{
    var domainRegEx = /^([^:]+:\/\/)([^\/]+)/;
    var domain_match = url.match(domainRegEx);

    return "http://"+domain_match[2];
}

pageMod.PageMod({
    include: [
        "http://bbs.auxten.co*",
        "http://bbs.stuhome*",
        "http://bbs.qshpan*",
        "http://bbs.germanyt*",
        "http://bbs.uestc6.edu*",
        "http://uc.stuhome.net/admin.php*"
    ],
    contentScriptWhen: "ready",
    contentScriptFile: [
        data.url("js/jquery-1.7.1.js"),
        data.url("js/proc/qsh_procs.js")
    ],
    onAttach: function(worker) {
        if(ss.storage.keep_login == 1) {
            var {Cc, Ci} = require("chrome");
            var IHazCookeh = {
                observe:function(subject, topic, data) {
                    let cookie = subject.QueryInterface(Components.interfaces.nsICookie);
                    var now = (new Date()).getTime()/1000;
                    var a_year = 365 * 24 * 60 * 60;

                    if (cookie.name == "56f61_winduser") {
                        if(cookie.expires - parseInt(now) < a_year) {
                            var current_href = "http://"+cookie.host;
                            var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
                            cm.remove(cookie.host, cookie.name, cookie.path, false);
                            var a_year = 365 * 24 * 60 * 60*1000;
                            var now = (new Date()).getTime();
                            var cookieString = "56f61_winduser="+cookie.value+";expires="+(new Date(now+a_year*10)).toUTCString();

                            var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
                            var cookieUri = ios.newURI(current_href, null, null);

                            var cookieSvc = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
                            cookieSvc.setCookieString(cookieUri, null, cookieString, null);
                        }
                    }

                }
            }
            var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
            observerService.addObserver(IHazCookeh, "cookie-changed", false);
        }
        require("request").Request({
            url: 'http://bbs.stuhome.net/thread.php?fid=1',
            onComplete: function(response) {
                if(response.status == "200")
                    ss.storage.in_uestc = 1;
                else
                    ss.storage.in_uestc = 0;
                worker.port.emit('init_page',ss.storage);
            }
        }).get();
    }
});

function proc_custom_bbs(data)
{
    if(data == 0) {
        ss.storage.rm_main_ad = 0;
        ss.storage.rm_bbs_rules = 0;
        ss.storage.hide_level_2 = 0;
    } else if(data == 1) {
        ss.storage.rm_main_ad = 1;
        ss.storage.rm_bbs_rules = 0;
        ss.storage.hide_level_2 = 0;
    } else if(data == 2) {
        ss.storage.rm_main_ad = 0;
        ss.storage.rm_bbs_rules = 1;
        ss.storage.hide_level_2 = 0;
    } else if(data == 3) {
        ss.storage.rm_main_ad = 1;
        ss.storage.rm_bbs_rules = 1;
        ss.storage.hide_level_2 = 0;
    } else if(data == 4) {
        ss.storage.rm_main_ad = 0;
        ss.storage.rm_bbs_rules = 0;
        ss.storage.hide_level_2 = 1;
    } else if(data == 5) {
        ss.storage.rm_main_ad = 1;
        ss.storage.rm_bbs_rules = 0;
        ss.storage.hide_level_2 = 1;
    } else if(data == 6) {
        ss.storage.rm_main_ad = 0;
        ss.storage.rm_bbs_rules = 1;
        ss.storage.hide_level_2 = 1;
    } else if(data == 7) {
        ss.storage.rm_main_ad = 1;
        ss.storage.rm_bbs_rules = 1;
        ss.storage.hide_level_2 = 1;
    }
}

function proc_all_block(data)
{
    var blocks=new Array();
    blocks = data.split('&');

    ss.storage.blk_persons = blocks[0];
    ss.storage.blk_signs = blocks[1];
    ss.storage.blk_heads = blocks[2];
    ss.storage.blk_pics = blocks[3];
    ss.storage.blk_articles = blocks[4];
}

function proc_logins(data)
{

    var log_sets=new Array();
    log_sets = data.split('&');

    ss.storage.inner = log_sets[0];
    ss.storage.outter = log_sets[1];
    ss.storage.auto_domain = log_sets[2];
    ss.storage.keep_login = log_sets[3];
    ss.storage.login_nowait = log_sets[4];
}

function proc_msg_from_usr(data)
{
    var msg_type;
    var tmp_array=new Array();

    tmp_array=data.split(':');
    msg_type = tmp_array[0];

    if(msg_type == "custom_bbs")
        proc_custom_bbs(tmp_array[1]);
    else if (msg_type == "all_block")
        proc_all_block(tmp_array[1]);
    else if(msg_type == "logins")
        proc_logins(tmp_array[1]);
}

pageMod.PageMod({
    include: "resource://cleanqsh-at-sans/cleanqsh/data/config*",
    contentScriptFile: [
        data.url("js/jquery-1.7.1.js"),
        data.url("js/proc/config.js")
    ],
    onAttach: function(worker) {
        worker.port.emit('init_page',ss.storage);
        worker.port.on('click', function(data) {
            proc_msg_from_usr(data);
        });
        worker.port.on('send_me_init_msg',function() {
            worker.port.emit('init_page',ss.storage);
        });
    }
});
